//
//  AddAddressViewController.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/1/15.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "AddAddressViewController.h"
//#import "CCZPickerView.h"
#import "DbDao.h"
#import "CityArea.h"
#import "SkyAssociationMenuView.h"
@interface AddAddressViewController ()<SkyAssociationMenuViewDelegate,UIAlertViewDelegate>
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UIScrollView *scroView;
@property (strong, nonatomic) IBOutlet UIButton *sureAddressButton;
@property (strong, nonatomic) IBOutlet UIButton *defaultAddressButton;
@property(nonatomic,strong)NSString * addressId;
@property (strong, nonatomic) IBOutlet UILabel *captionLabel;
@property (strong, nonatomic) IBOutlet UILabel *cityLabel;
@property (strong, nonatomic) IBOutlet UILabel *sectionLabel;

@property (strong, nonatomic) IBOutlet UITextField *zipCodeField;//邮编
@property (strong, nonatomic) IBOutlet UITextField *addressField;//地址
@property (strong, nonatomic) IBOutlet UITextField *nameField;//姓名
@property (strong, nonatomic) IBOutlet UITextField *phoneField;//电话
@property (strong, nonatomic) IBOutlet UIButton *cancleButton;

@property (strong, nonatomic) IBOutlet UIButton *headTitle;
@property(nonatomic,strong)NSMutableArray * captionArray;
@property(nonatomic,strong)NSMutableArray * cityArray;
@property(nonatomic,strong)NSMutableArray * sectionArray;
@property(nonatomic,strong)NSString * captionId;
@property(nonatomic,strong)NSString * cityId;
@property(nonatomic,strong)NSString * sectionId;

@property(nonatomic,strong)SkyAssociationMenuView * menuView;
@end

@implementation AddAddressViewController

- (void)viewWillAppear:(BOOL)animated{
    if (_addressDic.allKeys.count>0) {
        [_headTitle setTitle:@"编辑收货地址" forState:UIControlStateNormal];
        _defaultAddressButton.hidden = NO;
        _zipCodeField.text = _addressDic[@"msadEmail"];
        _addressField.text = _addressDic[@"msadAddr"];
        _nameField.text = _addressDic[@"msadReceiverName"];
        _phoneField.text = _addressDic[@"msadMobileNo"];
        [_sureAddressButton setTitle:@"保存" forState:UIControlStateNormal];
        [_cancleButton setTitle:@"删除" forState:UIControlStateNormal];
        [_cancleButton setTitleColor:[UIColor colorWithRed:0.980 green:0.278 blue:0.188 alpha:1.000] forState:UIControlStateNormal];
    }else{
        [_cancleButton setTitle:@"取消" forState:UIControlStateNormal];
         [_cancleButton setTitleColor:[UIColor colorWithRed:1.000 green:0.647 blue:0.035 alpha:1.000] forState:UIControlStateNormal];
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _captionArray = [NSMutableArray array];
    _sectionArray = [NSMutableArray array];
    _cityArray = [NSMutableArray array];
    DbDao *dao = [DbDao sharedInstance];
    NSArray *cityArr = [dao getCityArea:@"select * from sys_area where a_level = 3"];
    for (CityArea *area  in cityArr) {
        [_captionArray addObject:area.a_name];
    }
    
    [self initViewLayOut];
    // Do any additional setup after loading the view.
}

- (void)initViewLayOut{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroView];
    self.scroView.contentSize = flexibleSize(CGSizeMake(0, 568-63), NO);
    self.sureAddressButton.layer.cornerRadius = 16*VerticalRatio();
    self.defaultAddressButton.layer.cornerRadius = 16*VerticalRatio();
    _menuView = [[SkyAssociationMenuView alloc]initWithFrame:flexibleFrame(CGRectMake(90, 113, 230, 200), NO) lastClassTitle:@"返回上一级"];
    _menuView.delegate = self;
    
}
#pragma mark == 取消  货删除收货地址
- (IBAction)cancel:(UIButton *)sender {
    if([sender.titleLabel.text isEqualToString:@"取消"]){
        [self.navigationController  popViewControllerAnimated:YES];

    }else{
        NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
        
        [dic setObject:_addressDic[@"msadId"] forKey:@"ids"];
        self.shareApi.delegate=self;
        [self.shareApi  requestOperationWithDic:dic withPath:SX_deleteMallShippingAddress withHttpType:@"POST" withTag:SX_SX_deleteMallShippingAddress];
    }
}
- (IBAction)back:(id)sender {
    [self.navigationController  popViewControllerAnimated:YES];
}

- (IBAction)addAddress:(id)sender {
    [_menuView showAsDrawDownView];
}
#pragma mark == 设置默认地址
- (IBAction)setDefaultAddress:(id)sender {
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];

    [dic setObject:_addressDic[@"msadId"] forKey:@"msadId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_SetDefaultAddress withHttpType:@"POST" withTag:SX_SX_SetDefaultAddress];
}
#pragma mark == 确认地址
- (IBAction)sureAddress:(id)sender {
    
    if (_addressDic.allKeys.count>0) {//修改地址
        NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
        
        [dic setObject:_addressDic[@"msadId"] forKey:@"msadId"];
        [dic setObject:_zipCodeField.text forKey:@"msadPostcode"];
        [dic setObject:_addressField.text forKey:@"msadAddr"];
        [dic setObject:_nameField.text forKey:@"msadReceiverName"];
        [dic setObject:_phoneField.text forKey:@"msadMobileNo"];
        self.shareApi.delegate=self;
        [self.shareApi  requestOperationWithDic:dic withPath:SX_ChangeAddress withHttpType:@"POST" withTag:SX_SX_ChangeAddress];
    }else{//增加地址
        
        if ([Common dataIsNULL:_captionId]) {
            [[CWHUDTips sharedInstance]showTips:@"请选择省"];
            return;
        }
        if ([Common dataIsNULL:_cityId]) {
            [[CWHUDTips sharedInstance]showTips:@"请选择市"];
            return;
        }
        if ([Common dataIsNULL:_sectionId]) {
            [[CWHUDTips sharedInstance]showTips:@"请选择区"];
            return;
        }
        if ([Common dataIsNULL:_zipCodeField.text]) {
            [[CWHUDTips sharedInstance]showTips:@"请输入邮编"];
            return;
        }
        if ([Common dataIsNULL:_addressField.text]) {
            [[CWHUDTips sharedInstance]showTips:@"请输入详细地址"];
            return;
        }
        if ([Common dataIsNULL:_nameField.text]) {
            [[CWHUDTips sharedInstance]showTips:@"请输入收货人姓名"];
            return;
        }
        if ([Common dataIsNULL:_phoneField.text]) {
            [[CWHUDTips sharedInstance]showTips:@"请输入联系电话"];
            return;
        }
        
        
        NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
        NSDictionary * dic1 =  [Common getLoginInfo];
        [dic setObject:dic1[@"userId"] forKey:@"msadMemberId"];
        [dic setObject:_zipCodeField.text forKey:@"msadPostcode"];
        [dic setObject:_addressField.text forKey:@"msadAddr"];
        [dic setObject:_nameField.text forKey:@"msadReceiverName"];
        [dic setObject:_phoneField.text forKey:@"msadMobileNo"];
        [dic setObject:_captionId forKey:@"msadProvinceId"];
        [dic setObject:_captionLabel.text forKey:@"msadProvinceName"];
        [dic setObject:_cityId forKey:@"msadCityId"];
        [dic setObject:_cityLabel.text forKey:@"msadCityName"];
        [dic setObject:_sectionId forKey:@"msadAreaId"];
        [dic setObject:_sectionLabel.text forKey:@"msadAreaName"];
        self.shareApi.delegate=self;
        [self.shareApi  requestOperationWithDic:dic withPath:SX_AddAddress withHttpType:@"POST" withTag:SX_SX_AddAddress];
    }
}





- (NSInteger)assciationMenuView:(SkyAssociationMenuView*)asView countForClass:(NSInteger)idx {
    if (idx == 0) {
        return _captionArray.count;
    }else if (idx == 1){
        return _cityArray.count;
    }else{
        return _sectionArray.count;
    }
}

- (NSString*)assciationMenuView:(SkyAssociationMenuView*)asView titleForClass_1:(NSInteger)idx_1 {
    
    return _captionArray[idx_1];
}

- (NSString*)assciationMenuView:(SkyAssociationMenuView*)asView titleForClass_1:(NSInteger)idx_1 class_2:(NSInteger)idx_2 {
    
    return _cityArray[idx_2];
}

- (NSString*)assciationMenuView:(SkyAssociationMenuView*)asView titleForClass_1:(NSInteger)idx_1 class_2:(NSInteger)idx_2 class_3:(NSInteger)idx_3 {
    return _sectionArray[idx_3];
}

- (BOOL)assciationMenuView:(SkyAssociationMenuView*)asView idxChooseInClass1:(NSInteger)idx_1{
    _captionLabel.text = _captionArray[idx_1];
    DbDao *dao = [DbDao sharedInstance];
    NSArray *cityArr = [dao getCityArea:[NSString stringWithFormat:@"select * from sys_area where a_name = '%@'",_captionArray[idx_1] ]];
    CityArea *area = cityArr[0];
    NSArray *areaArr = [dao getCityArea:[NSString stringWithFormat:@"select * from sys_area where a_parent_id = %ld",area.a_id]];
    _captionId = [NSString stringWithFormat:@"%ld",area.a_id];
    [_cityArray removeAllObjects];
    for (CityArea *area  in areaArr) {
        [_cityArray addObject:area.a_name];
    }
    return YES;
}

- (BOOL)assciationMenuView:(SkyAssociationMenuView*)asView idxChooseInClass1:(NSInteger)idx_1 class2:(NSInteger)idx_2{
    _cityLabel.text = _cityArray[idx_2];
    DbDao *dao = [DbDao sharedInstance];
    NSArray *cityArr = [dao getCityArea:[NSString stringWithFormat:@"select * from sys_area where a_name = '%@'",_cityArray[idx_2]]];
    CityArea *area = cityArr[0];
    NSArray *areaArr = [dao getCityArea:[NSString stringWithFormat:@"select * from sys_area where a_parent_id = %ld",area.a_id]];
    _cityId = [NSString stringWithFormat:@"%ld",area.a_id];
    [_sectionArray removeAllObjects];
    for (CityArea *area  in areaArr) {
        [_sectionArray addObject:area.a_name];
    }
    
    return YES;
}

- (BOOL)assciationMenuView:(SkyAssociationMenuView*)asView idxChooseInClass1:(NSInteger)idx_1 class2:(NSInteger)idx_2 class3:(NSInteger)idx_3{
    _sectionLabel.text = _sectionArray[idx_3];
    DbDao *dao = [DbDao sharedInstance];
    NSArray *cityArr = [dao getCityArea:[NSString stringWithFormat:@"select * from sys_area where a_name = '%@'",_sectionArray[idx_3]]];
    CityArea *area = cityArr[0];
    _sectionId = [NSString stringWithFormat:@"%ld",area.a_id];

    return YES;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        
        NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
        
        [dic setObject:_addressId forKey:@"msadId"];
        self.shareApi.delegate=self;
        [self.shareApi  requestOperationWithDic:dic withPath:SX_SetDefaultAddress withHttpType:@"POST" withTag:SX_SX_SetDefaultAddress];
    }else{
        [self.navigationController  popViewControllerAnimated:YES];
    }

}

#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    
    
    if (tag==SX_SX_AddAddress) {
        
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [[CWHUDTips sharedInstance]  showTips:@"添加成功"  withStatus:1 dismissAfter:1.5];
            _addressId = database[@"result"][@"msadId"];
            UIAlertView * alterView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"是否将该地址设置为默认收货地址" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alterView show];
        
        }
        
    }else if (tag==SX_SX_ChangeAddress) {
        
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [[CWHUDTips sharedInstance]  showTips:@"修改地址成功"  withStatus:1 dismissAfter:2];
            [self.navigationController  popViewControllerAnimated:YES];
        }
        
    }else if (tag==SX_SX_SetDefaultAddress) {
        
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [[CWHUDTips sharedInstance]  showTips:@"设置默认地址成功"  withStatus:1 dismissAfter:2];
            [self.navigationController  popViewControllerAnimated:YES];
            
        }
        
    }else if (tag == SX_SX_deleteMallShippingAddress){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [self.navigationController popViewControllerAnimated:YES];
             [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  ];
            
            
        }
    }
}
-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    //
    [self dismissWaitDialog];
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
